package com.ea.common.node;


import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;



/**
 * @author jun
 * @Description 菜单的节点
 */
public class MenuNode {

    /**
     * 节点id
     */
    private String id;

    /**
     * 父节点
     */
    private String parentId;

    /**
     * 节点名称
     */
    private String name;


    /**
     * 按钮的排序
     */
    private Integer sort;

    /**
     * 节点的url
     */
    private String href;
    

    /**
     * 节点图标
     */
    private String icon;
    
    
    /**
     * 权限定义
     */
    private String permission;
    

    /**
     * 子节点的集合
     */
    private List<MenuNode> children;

    /**
     * 查询子节点时候的临时集合 暂时不要了，资源不释放会越来越多
     */
   // private  static List<MenuNode> listAll = new ArrayList<MenuNode>();

    
    public MenuNode() {
        super();
    }

    public MenuNode(String id, String parentId) {
        super();
        this.id = id;
        this.parentId = parentId;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public static MenuNode createRoot() {
        return new MenuNode("0", "-1");
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public List<MenuNode> getChildren() {
        return children;
    }

    public void setChildren(List<MenuNode> children) {
        this.children = children;
    }

    



	@Override
	public String toString() {
		return "MenuNode [id=" + id + ", parentId=" + parentId + ", name=" + name + ", sort=" + sort + ", href=" + href
				+ ", icon=" + icon + ", permission=" + permission + ", children=" + children + "]";
	}

	/**
     * 构建整个菜单树
     *
     * @author jun
     */
    public void buildNodeTree(List<MenuNode> nodeList) {
    	
        for (MenuNode treeNode : nodeList) {
            List<MenuNode> linkedList = treeNode.findChildNodes(nodeList, treeNode.getId());
            if (linkedList.size() > 0) {
                treeNode.setChildren(linkedList);
            }
        }
    }

    /**
     * 查询子节点的集合
     *
     * @author jun
     */
    public List<MenuNode> findChildNodes(List<MenuNode> nodeList, String parentId) {
        if (nodeList == null && parentId == null)
            return null;
        
        List<MenuNode> child=new ArrayList<MenuNode>();
       
        for (Iterator<MenuNode> iterator = nodeList.iterator(); iterator.hasNext(); ) {
            MenuNode node = (MenuNode) iterator.next();
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (node.getParentId() != "0" && parentId.equals(node.getParentId())) {
               // recursionFn(nodeList, node, parentId);
            	recursionFn(child,nodeList, node, parentId);
            }
        }
        return child;
    }

    /**
     * 遍历一个节点的子节点
     *
     * @author jun
     */
    public void recursionFn(List<MenuNode> cList,List<MenuNode> nodeList, MenuNode node, String pId) {
    	
    
        List<MenuNode> childList = getChildList(nodeList, node);// 得到子节点列表
        
        if (childList.size() > 0) {// 判断是否有子节点
            if (node.getParentId().equals(pId)) {
            	//listAll.add(node);
            	cList.add(node);
            	
            }
            Iterator<MenuNode> it = childList.iterator();
            while (it.hasNext()) {
                MenuNode n = (MenuNode) it.next();
                recursionFn(cList,nodeList, n, pId);
            }
        } else {
            if (node.getParentId().equals(pId)) {
            	//listAll.add(node);
            	cList.add(node);
            }
        }
        
        
    }

    /**
     * 得到子节点列表
     *
     * @author jun
     */
    private List<MenuNode> getChildList(List<MenuNode> list, MenuNode node) {
        List<MenuNode> nodeList = new ArrayList<MenuNode>();
        Iterator<MenuNode> it = list.iterator();
        while (it.hasNext()) {
            MenuNode n = (MenuNode) it.next();
            if (n.getParentId().equals(node.getId())) {
                nodeList.add(n);
            }
        }
        return nodeList;
    }

 
    /**
     * 清除所有二级菜单
     *
     */
    public static List<MenuNode> clearLevelTwo(List<MenuNode> nodes) {
        List<MenuNode> results = new ArrayList<MenuNode>();
        for (MenuNode node : nodes) {
            String pid=node.getParentId();
            if (pid.equals("1")) {
                results.add(node);
            }
        }
        return results;
    }
    
    
    
    /**
     * 构建菜单列表
     *
     * @date 2017年2月19日 下午11:18:19
     */
    public static List<MenuNode> buildTitle(List<MenuNode> nodes) {

    	  new MenuNode().buildNodeTree(nodes);

    	  List<MenuNode> menulist = clearLevelTwo(nodes);
           

        return menulist;
    }
    
    


    
}
